// pages/order/refund/refund.js
import api from '../../../utils/api'
import message from '../../../utils/message';
import util from '../../../utils/util'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        order_products:[],
        order_no:null,
        has_load:false,
        show_action_menus:false,
        show_refund_reason:false,
        refund_reason_list:[],
        reason_id:-1,
        reason:'',
        image_list:[],
        show_refund_money:false,
        type:1,
        refund_product:null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {


        if(options.order_no){
            var order_no = options.order_no;

            this.setData({
                order_no : order_no
            });
            this.getOrderProducts(order_no)
        }else{

            message.show_error("访问异常，请确认订单号是否存在" , function () {
                wx.navigateBack({
                  delta: 1
                })
                
            })

        }


        this.getRefundReasonList();


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getOrderProducts:function (order_no) {
        var me = this;

        var params = {};
        params.order_no = order_no;
        api.getOrderProducts( params , function(response) {
            if(response.status==1){
                me.setData({
                    has_load:true,
                    order_info:response.data.order_info,
                    order_products: response.data.products
                })
            }else{

                message.show_error( response.message )

            } 
        })
    },
    getRefundReasonList:function (response) {
        var me = this;
        api.refund.getRefundReasonList({} , function (response) {
            if(response.status==1){
                me.setData({
                    refund_reason_list:response.data
                })
            }else{
                message.show_error( response.message )
            }
        })
        
    },
    doOrderRefund:function ( e) {
        //点击申请退货
        var order_no = this.data.order_no;

        var action= e.currentTarget.dataset.action;

        if(action==-1){
            this.setData({
                show_action_menus:false
            })
        }

        if(action==1){
            this.setData({
                show_action_menus:true
            })
        }

        /*

        var params = {};
        params.order_no = order_no;
        params.reason = "鞋子有点破旧，想换双新的";
        params.product_id = 11;
        api.doOrderRefund( params , function (response) {

            if(response.status==1){
                message.show_error( response.message , function() {
                    wx.navigateBack({
                      delta: 1,
                    })
                    
                })

            }else{
                message.show_error( response.message );
            }
            
        } )

        */
    },
    makePhoneCall:function(){
        wx.makePhoneCall({
          phoneNumber: '18102115872',
        })
    },
    selectRefundReason:function (e) {
        var action = e.currentTarget.dataset.action;
        if(action==1){
            this.setData({
                show_refund_reason:true
            })
        }
        if(action==-1){
            this.setData({
                show_refund_reason:false
            })
        }

        if(action==2){
            var id = e.currentTarget.dataset.id;
            var reason = e.currentTarget.dataset.reason;
            this.setData({
                reason_id: id,
                reason: reason,
                show_refund_reason:false
            })

        }
        
    },
    chooseImage:function () {
        var me = this;

        util.chooseImage( function (response) {
            me.uploadImage( response );
            
        })
        
    },
    uploadImage:function (temp_file) {

        console.log("upload_comment_image")
        var me = this;
        api.user.uploadCommentImage( temp_file , {} , function( response ){
            console.log(response);
            var res = JSON.parse(response);
            //cb(response.data);
            
            if(res.status==1){
                var image_list = me.data.image_list;
                image_list.push( res.data.url)

                console.log(image_list);
                me.setData({
                    image_list: image_list
                })
            }
        })
        
    },
    showRefundMoney:function (e) {
        var action= e.currentTarget.dataset.action;
        var type = e.currentTarget.dataset.type;
        
        if(action==1){

            if(type!=1){
                var index = e.currentTarget.dataset.index;

                var refund_product = this.data.order_products[index]

                this.setData({
                    show_refund_money: true,
                    type:type,
                    refund_product:refund_product
                })

            }else{
                this.setData({
                    show_refund_money: true,
                    type:type,
                    refund_product:null
                })
            }
            
        }

        if(action==-1){
            this.setData({
                show_refund_money:false
            })
        }

        if(action==2){

            var data = {};

            data.order_no = this.data.order_no;
            data.product_id = 1;  //只有退换商品时候才用到此订单

            if(this.data.reason_id==-1){
                message.show_error("请选择退款原因");
                return ;
            }

            if(this.data.image_list.length<=0){
                message.show_error("请上传3张以上凭证");
                return ;
            }

            var type = this.data.type;
            if(type==2 || type==3){

                data.product_id = this.data.refund_product.product_id;
                data.product_nums = this.data.refund_product.product_nums
            }

            data.reason = this.data.reason;
            data.reason_id = this.data.reason_id;
            data.image_list = this.data.image_list.join(",");
            data.type = this.data.type; //退单申请

            api.refund.createRefundOrder( data  , function (response) {
                if(response.status==1){
                    message.show_error(response.message);
                }else{
                    message.show_error(response.message);
                }
                
            } )
            
        }

        
    },
    showRefundProduct:function (e) {
        //退换货物
        
    },
    showRefundOrder:function(e) {
        //退订单
        
    }
})